package org.jsets.fastboot.security.authc;

import java.util.Map;
import org.jsets.fastboot.security.config.Constants;
import org.jsets.fastboot.util.SpringContextHolder;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LoginHandlerManager {
	
	private ILoginHandler defaultLoginHandler;
	private final Map<String, ILoginHandler> handlers = Maps.newHashMap();

	public void afterInitialize() {
		SpringContextHolder.tryBeansThen(ILoginHandler.class, map -> {
			map.values().forEach(handler -> {
				log.info("Authenticator[{}:{}]", handler.getSupportAuthcType(), handler.getClass().getSimpleName());
				if (Constants.DEFAULT_AUTHC_TYPE.equals(handler.getSupportAuthcType())) {
					this.defaultLoginHandler = handler;
				} else {
					this.handlers.put(handler.getSupportAuthcType(), handler);
				}
			});
		});
	}
	
	public ILoginHandler getLoginHandler(String authcType) {
		if (this.handlers.size() == 0) {
			return this.defaultLoginHandler;
		}
		return this.handlers.get(authcType);
	}

}